package com.srit.map.web.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.srit.core.struts2.action.BaseAction;
import com.srit.map.business.bean.Menu;
import com.srit.map.business.service.MenuService;
import com.srit.map.business.service.impl.MenuServiceImpl;
import com.srit.map.business.tree.Node;

/**
 * @author chenxiling
 *
 */
public class MenuAction extends BaseAction{
	
	@Autowired(required = true)
	MenuService menuService=new MenuServiceImpl();
	
	/**
	 * 得到MenuTree输出Json
	 */
	public String menuTree(){
		
		List<Menu> menuList=menuService.getMenuList();
		response.setContentType("application/json;charset=UTF-8");
		List<Node> NodeList=new ArrayList();
		
		for(Menu menu:menuList){
			Node node=new Node();
			node.setId(menu.getId());
			node.setName(menu.getName());
			node.setpId(menu.getParentId());
			node.setUrl(menu.getUrl());
			if(menu.getParentId()==0){
				node.setOpen(true);
			}
			NodeList.add(node);
		}
		PrintWriter out;
		try {
			String str=JSONUtil.serialize(NodeList);
			out = response.getWriter();
			out.println(str);
			out.flush();
			out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String jump(){
		return "success";
	}
	
}
